package com.restkeeper.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;

/**
 * 返回结果通用封装
 */
@Data
public class Result {
    // 返回状态
    private int code;
    // 状态描述
    private String desc;
    // 返回数据
    private Object data;

    private String token;

    public Result(){}

    public static Result error(){
        Result result = new Result();
        result.code=ResultCode.error;
        result.desc="请求错误";
        JSONObject object=new JSONObject();
        object.put("code","failed");
        return result;
    }

    public static Result error(int errorCode,String desc){
        Result result = new Result();
        result.code=errorCode;
        result.desc=desc;
        JSONObject object=new JSONObject();
        object.put("code","failed");
        result.data=object;
        return result;
    }

    public static Result error(String desc){
        Result result = new Result();
        result.code=ResultCode.error;
        result.desc=desc;
        return result;
    }

    public static Result ok(Object data,String token){
        Result result = new Result();
        result.code=ResultCode.success;
        result.data=data;
        result.desc="ok";
        result.token=token;
        return result;
    }

    public static Result ok(){
        Result result = new Result();
        result.code=ResultCode.success;
        result.desc="ok";
        JSONObject object=new JSONObject();
        object.put("code","success");
        result.data=object;
        return result;
    }

    public static Result ok(Object data){
        Result result = new Result();
        result.code=ResultCode.success;
        result.desc="ok";
        result.data=data;
        return result;
    }


}